<template>
	<view>
		<view class="container">
			<view class="temperature">
				<text class="temperature-text">温度为：{{ temperature }}°C</text>
			</view>
			<view class="buttons">
				<button @click="increaseTemperature" class="button">增加温度</button>
				<button @click="decreaseTemperature" class="button">减少温度</button>
				<button @click="queryTemperature" class="button">查询温度</button>
			</view>
		</view>
	</view>
</template>

<script>
	import mqtt from '@/utils/mqtt.js'
	const mqttll = new mqtt()
	var val_arr = [0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
	export default {
		data() {
			return {
				temperature: '', //
				index: 1,
				arr: [],
				dictArray: [{
						'00': '15'
					},
					{
						'01': '16'
					},
					{
						'02': '17'
					},
					{
						'03': '18'
					},
					{
						'04': '19'
					},
					{
						'05': '20'
					},
					{
						'06': '21'
					},
					{
						'07': '22'
					},
					{
						'08': '23'
					},
					{
						'09': '24'
					},
					{
						'0a': '25'
					},
					{
						'0b': '26'
					},
					{
						'0c': '27'
					},
					{
						'0d': '28'
					},
					{
						'0e': '29'
					},
					{
						'0f': '30'
					},
					{
						'10': '31'
					},
					{
						'11': '32'
					},
					{
						'12': '33'
					},
					{
						'13': '34'
					}
				]
			}
		},
		mounted() {
			uni.$on('data_android2can', this.massage)
			//开启空调命令02,04,01
			//查询温度命令02,02,00
			mqttll.sendMes('can2android', '02,04,01,00,00,00,00,00')
			uni.setStorageSync("index", 2)
			this.queryTemperature()
		},

		methods: {
			increaseTemperature() {
				this.index = uni.getStorageSync("index")
				console.log(this.index);
				if (this.index == "") {
					uni.setStorageSync("index", 2)
				}		
				
				this.index += 1
				let wd = '0' + this.index
				uni.setStorageSync("index", this.index)
				if (this.index >= 10) {
					if (this.index == 10) {
						wd = '0a'
					}
					if (this.index == 11) {
						wd = '0b'
					}
					if (this.index == 12) {
						wd = '0c'
					}
					if (this.index == 13) {
						wd = '0d'
					}
					if (this.index == 14) {
						wd = '0e'
					}
					if (this.index == 15) {
						wd = '0f'
					}
					if (this.index == 16) {
						wd = '10'
					}
					if (this.index == 17) {
						wd = '11'
					}

				}
				mqttll.sendMes('can2android', `02,05,${wd},00,00,00,00,00`)
				///需不需要重新发送
				this.queryTemperature()
			},
			decreaseTemperature() {
				this.index = uni.getStorageSync("index")
				console.log(this.index);
				if (this.index == "") {
					uni.setStorageSync("index", 2)
				}		
				
				this.index -= 1
				let wd = '0' + this.index
				uni.setStorageSync("index", this.index)
				if (this.index >= 10) {
					if (this.index == 10) {
						wd = '0a'
					}
					if (this.index == 11) {
						wd = '0b'
					}
					if (this.index == 12) {
						wd = '0c'
					}
					if (this.index == 13) {
						wd = '0d'
					}
					if (this.index == 14) {
						wd = '0e'
					}
					if (this.index == 15) {
						wd = '0f'
					}
					if (this.index == 16) {
						wd = '10'
					}
					if (this.index == 17) {
						wd = '11'
					}
				
				}
				mqttll.sendMes('can2android', `02,05,${wd},00,00,00,00,00`)
				///需不需要重新发送
				this.queryTemperature()
			},

			massage(data) {
				console.log("触发");
				this.arr = data.msg.split(",")
				if (this.arr[1] == 2) {
					for (var i = 0; i < this.dictArray.length; i++) {
						console.log(this.dictArray[i])
						var my_dict = this.dictArray[i]
						for (var key in my_dict) {
							if (this.arr[2] == key) {
								this.temperature = my_dict[key]
								console.log("key:" + key)
								console.log("val:" + my_dict[key])

							}

						}

					}

				} else if (this.arr[1] == 5) {
					for (var i = 0; i < this.dictArray.length; i++) {
						console.log(this.dictArray[i])
						var my_dict = this.dictArray[i]
						for (var key in my_dict) {
							if (this.arr[2] == key) {
								this.temperature = my_dict[key]
								console.log("key:" + key)
								console.log("val:" + my_dict[key])

							}
						}
					}
					//index值处理
					var index1 = uni.getStorageSync("index")
					if (Number(this.arr[2]) > index1) {															
						uni.setStorageSync("index", ++index1)
						//console.log("存到本地的index="+uni.getStorageSync("index"))
					} else {
						uni.setStorageSync("index", --index1)
					}


				}



			},
			queryTemperature() {
				mqttll.sendMes('can2android', `02,02,00,00,00,00,00,00`)
			}

		}
	}
</script>

<style>

</style>
